<?php

namespace Eduxplus\CmsBundle\Service\Admin;

use Eduxplus\CmsBundle\Entity\CmsTutorialChapter;
use Eduxplus\CoreBundle\Lib\Base\AdminBaseService;
use Knp\Component\Pager\PaginatorInterface;

class TutorialChapterService extends AdminBaseService
{
    protected $paginator;
    protected $tutorialService;

    public function __construct(
        PaginatorInterface $paginator,
        TutorialService $tutorialService
    ) {
        $this->tutorialService = $tutorialService;
        $this->paginator = $paginator;
    }

    public function getChapterTree($parentId, $tutorialId)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.parentId = :parentId AND a.tutorialId = :tutorialId ORDER BY a.sort ASC";
        $items = $this->db()->fetchAll($sql, ['parentId' => $parentId, "tutorialId" => $tutorialId]);
        if (!$items) return [];
        $result = [];
        foreach ($items as &$v) {
            $tmp = $v;
            $child =  $this->getChapterTree($v['id'], $tutorialId);
            $tmp['childs'] = $child;
            $result[] = $tmp;
        }
        return $result;
    }


    public function updateSort($data, $pid = 0)
    {
        if ($data) {
            $sort = 0;
            foreach ($data as $k => $v) {
                $id = $v['id'];
                $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.id=:id";
                $model = $this->db()->fetchOne($sql, ['id' => $id], 1);
                $model->setSort($sort);
                $model->setParentId($pid);
                $this->db()->save($model);
                $sort++;
                if (isset($v['children'])) {
                    $this->updateSort($v['children'], $id);
                }
            }
        }
    }

    public function getAllChapter($tutorialId)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.tutorialId=:tutorialId ORDER BY a.sort ASC";
        $list = $this->db()->fetchAll($sql, ["tutorialId" => $tutorialId]);
        if (!$list) return [];

        $rs = [];
        foreach ($list as $v) {
            $pid = $v['parentId'];
            $rs[$pid][] = $v;
        }
        return $rs;
    }

    public function chapterSelect($tutorialId)
    {
        $all = $this->getAllChapter($tutorialId);
        $rs = [];
        $rs['/'] = 0;
        if ($all) {
            foreach ($all[0] as $vv) {
                $id = $vv['id'];
                $name = "┝&nbsp;" . $vv['name'];
                $rs[$name] = $id;
                //                if (isset($all[$id])) {
                //                    $pre = "&nbsp;&nbsp;&nbsp;&nbsp;";
                //                    $this->_Select($all, $id, $pre, $rs);
                //                }
            }
        }
        return $rs;
    }

    protected function _Select($all, $id, $pre, &$rs)
    {
        if ($all[$id]) {
            foreach ($all[$id] as $v) {
                $id = $v['id'];
                $name = $pre . "┝&nbsp;" . $v['name'];
                $rs[$name] = $id;
                if (isset($all[$id])) {
                    $pre = $pre . "&nbsp;&nbsp;&nbsp;&nbsp;";
                    $this->_Select($all, $id, $pre, $rs);
                }
            }
        }
    }

    public function add($name, $parentId, $sort, $id, $status,$tags)
    {
        $info = $this->tutorialService->getById($id);
        $path = $this->findPath($parentId);
        $model = new CmsTutorialChapter();
        $model->setName($name);
        $model->setParentId($parentId);
        $model->setType($info['type']);
        $model->setSort($sort);
        $model->setTutorialId($id);
        $model->setPath($path);
        $model->setStatus($status);
        $model->setTags($tags);

        $chapterId = $this->db()->save($model);

        return $chapterId;
    }

    public function findPath($id)
    {
        if (!$id) return ",";
        $sql = "SELECT a.parentId FROM Cms:CmsTutorialChapter a WHERE a.id = :id";
        $pid = $this->db()->fetchField("parentId", $sql, ['id' => $id]);
        if (!$pid) return ",{$id},";
        $str = ",{$id},";
        $str .= ltrim($this->findPath($pid), ",");
        return $str;
    }

    public function editContent($id, $content)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.id=:id";
        $model = $this->db()->fetchOne($sql, ['id' => $id], 1);
        $model->setContent($content);
        $this->db()->save($model);
    }

    public function edit($id, $name,  $parentId, $sort, $status,$tags)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.id=:id";
        $model = $this->db()->fetchOne($sql, ['id' => $id], 1);

        $path = $this->findPath($parentId);

        $model->setPath($path);
        $model->setName($name);
        $model->setParentId($parentId);

        $model->setSort($sort);
        $model->setStatus($status);
        $model->setTags($tags);
        $this->db()->save($model);
    }

    public function getById($id)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.id=:id";
        return $this->db()->fetchOne($sql, ['id' => $id]);
    }


    public function getChapter($chapterId)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.id=:id";
        $info = $this->db()->fetchOne($sql, ["id" => $chapterId]);
        if (!$info) return [];
        return $info;
    }


    public function del($id)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.id=:id";
        $model = $this->db()->fetchOne($sql, ['id' => $id], 1);
        return $this->db()->delete($model);
    }

    public function hasChild($id)
    {
        $sql = "SELECT a FROM Cms:CmsTutorialChapter a WHERE a.parentId=:parentId";
        return $this->db()->fetchOne($sql, ['parentId' => $id]);
    }
}
